/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import java.io.IOException;
import java.math.BigInteger;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import myconfession.dao.PostDAO;
import myconfession.dto.post.Post;
import myconfession.dto.post.PostList;
import myconfession.utils.ConstantProvider;
import myconfession.utils.XMLUtility;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Liem
 */
public class PostBO {

    public static String UNREAD = "Unread";
    public static String READ = "Read";
    public static String APPROVED = "Approved";

    public static void loadAllAndMarshall(String filePath) throws JAXBException {
        PostList list = PostDAO.getPostList();
        XMLUtility.marshallJAXB(filePath, list);
    }

    public static Post getPostById(BigInteger id) throws JAXBException {
        PostList postList = (PostList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), PostList.class);
        for (Post tmp : postList.getPost()) {
            if (tmp.getId().intValue() == id.intValue()) {
                return tmp;
            }
        }
        return null;
    }

    public static int getNumberOfPosted(BigInteger pageId) throws JAXBException {
        int countPosted =0;
        PostList postList = (PostList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), PostList.class);
        for (Post tmp : postList.getPost()) {
            if (tmp.getPageId().intValue() == pageId.intValue()) {
                if (tmp.getStatus().equals(PostBO.APPROVED)) {
                    countPosted++;
                }
            }
        }
        return countPosted;
    }

    //Bao LA
    public static boolean AddPost(Post p) throws JAXBException {
        int post = PostDAO.sendPost(p);
        if (post > 0) {
            p.setId(new BigInteger(post + ""));
            PostList pl = (PostList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), PostList.class);
            pl.getPost().add(p);
            XMLUtility.marshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), pl);
            return true;
        }
        return false;
    }

    //Liem
    public static void approvePost(int id, String fbPostId) throws JAXBException {
        PostList pl = (PostList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), PostList.class);
        for (Post post : pl.getPost()) {
            if (post.getId().intValue() == id) {
                if (PostDAO.updatePost(id, post.getPostContent(), APPROVED, fbPostId)) {
                    post.setStatus(APPROVED);
                    post.setFbPostId(fbPostId);
                    break;
                }
            }
        }
        XMLUtility.marshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), pl);
    }

    // Liem
    public static void unpost(int id) throws JAXBException {
        PostList pl = (PostList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), PostList.class);
        for (Post post : pl.getPost()) {
            if (post.getId().intValue() == id) {
                if (PostDAO.updatePost(id, post.getPostContent(), READ, null)) {
                    post.setStatus(READ);
                    post.setFbPostId(null);
                    break;
                }
            }
        }
        XMLUtility.marshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), pl);
    }

    //Bao
    public static void rejectPost(int id) throws ParserConfigurationException,
            SAXException, IOException, TransformerConfigurationException, TransformerException {
        if (PostDAO.rejectPost(id)) {
            Document dom = XMLUtility.parseDOMfromFile(ConstantProvider.getInstance().getPostXmlPath());
            rejectPost(dom, id);
            XMLUtility.writeDOMtoXml(dom, ConstantProvider.getInstance().getPostXmlPath());
        }
    }

    public static void rejectPost(Node node, int id) {
        if (node == null) {
            return;
        }
        if (node.getNodeName().equals("id")) {
            if (node.getTextContent().trim().equals(Integer.toString(id))) {
                Node parent = node.getParentNode();
                NodeList child = parent.getChildNodes();
                for (int i = 0; i < child.getLength(); i++) {
                    Node tmp = child.item(i);
                    if (tmp.getNodeName().equals("status")) {
                        tmp.setTextContent(READ);
                        return;
                    }
                }
            }
        }
        NodeList child = node.getChildNodes();
        for (int i = 0; i < child.getLength(); i++) {
            rejectPost(child.item(i), id);
        }
    }


    //Liem
    public static void updatePost(int id, String content) throws JAXBException {
        PostList pl = (PostList) XMLUtility.unmarshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), PostList.class);
        for (Post post : pl.getPost()) {
            if (post.getId().intValue() == id) {
                if (PostDAO.updatePost(id, content, post.getStatus(), post.getFbPostId())) {
                    post.setPostContent(content);
                    break;
                }
            }
        }
        XMLUtility.marshallJAXB(ConstantProvider.getInstance().getPostXmlPath(), pl);
    }
}
